<template>
    <view class="list">
        <form @submit="formSubmit">
            <view class="item">
                <label>姓名</label>
                <input :value="userName" name="userName" placeholder="姓名" @input="nameinput" />
            </view>
            <view class="item bt">
                <button formType="submit" class="btn" v-if="!noSave" :disabled="noSave">保存</button>
            </view>
        </form>
    </view>
</template>

<script>
var config = require('../../../config.js');
var http = require('../../../utils/httpHelper.js');
export default {
    data() {
        return {
            userName: '',
            noSave: true
        };
    }, //事件处理函数
    onLoad: function () {
        var that = this;
        let userInfo = uni.getStorageSync('user');
        that.setData({
            userName: userInfo.userName
        });
    },
    methods: {
        nameinput: function (e) {
            this.setData({
                userName: e.detail.value,
                noSave: false
            });
        },

        formSubmit: function (e) {
            //提交表单
            let userInfo = uni.getStorageSync('user');
            var val = e.detail.value;
            if (val.userName != '') {
                var data = {
                    userId: userInfo.userId,
                    userName: val.userName
                };
                http.httpPutForm(config.clubApi.putUserName, data, function (res) {
                    if (res.code == '200') {
                        userInfo.userName = val.userName;
                        uni.setStorageSync('user', userInfo);
                        uni.navigateBack();
                        console.log('修改姓名成功');
                    } else {
                        console.log('修改姓名失败');
                    }
                });
            }
        }
    }
};
</script>
<style lang="scss">
@import './index.scss';
</style>
